
import java.io.IOException;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.game.GameCanvas;
import javax.microedition.lcdui.game.LayerManager;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
/**
 *
 * @author personal
 */
public class SplashScreen extends GameCanvas implements Runnable {

    private NinjaRunMIDlet midlet;
    private AnimatedSprite spShadow;
    private static int FRAME_SHADOW_WIDTH = 0;
    private static int FRAME_SHADOW_HEIGHT = 0;
    private LayerManager lmgr;
    private String pathShadow = "";
    private String pathTitle = "";    
    private Image imgTitle;
    private boolean running = false;
    private int loop = 0;
    Thread thread = null;
    //xy
    private int xShadow = 0;
    private int yShadow = 0;
    private int xTitle = 0;
    private int yTitle = 0;
    private int xString = 0;
    private int yString = 0;        
    //speed
    private int speedGame = 50;
    
    //disable and paused
    //boolean
    private boolean isDisablePortrait = false;
    private boolean isPaused = false;
    //button
    private ButtonImage btnResume;    
    //image
    private Image imgBtnResume;
    private Image imgDisablePortrait;    
    private int acuanDisable = 320;
    private String pathBtnResume = "";    
    private String pathDisable = "/disableportrait.png";
    

    public SplashScreen(NinjaRunMIDlet host) {
        super(true);
        this.midlet = host;
        setFullScreenMode(true);
        
        if (getWidth() == 640) {
            FRAME_SHADOW_WIDTH = 46;
            FRAME_SHADOW_HEIGHT = 49;
            pathShadow = "/black-shadow640.png";
            pathTitle = "/titleNinjaRun640.png";
            pathBtnResume = "/btnResume640.png";
        } else if (getWidth() > acuanDisable) {            
            FRAME_SHADOW_WIDTH = 46;
            FRAME_SHADOW_HEIGHT = 49;
            pathShadow = "/black-shadow640.png";
            pathTitle = "/titleNinjaRun640.png";
            pathBtnResume = "/btnResume640.png";
            isDisablePortrait = true;
            isPaused = true;
        } else {
            FRAME_SHADOW_WIDTH = 23;
            FRAME_SHADOW_HEIGHT = 25;
            pathShadow = "/black-shadow320.png";
            pathTitle = "/titleNinjaRun320.png";
            pathBtnResume = "/btnResume320.png";
        }

        try {  
            imgTitle = Image.createImage(pathTitle);
            imgBtnResume = Image.createImage(pathBtnResume);
            imgDisablePortrait = Image.createImage(pathDisable);

            int xyArea[] = new int[4];
            
            int xResume = getWidth() - imgBtnResume.getWidth();
            int yResume = getHeight() - imgBtnResume.getHeight();            
            xyArea[0] = xResume; //x1
            xyArea[1] = xResume + imgBtnResume.getWidth(); //x2
            xyArea[2] = yResume; //y1
            xyArea[3] = yResume + imgBtnResume.getHeight(); //y2
            btnResume = new ButtonImage("resume", imgBtnResume, imgBtnResume,
                    imgBtnResume, xResume,
                    yResume, xyArea, Graphics.TOP | Graphics.LEFT);

            //animated sprite
            spShadow = new AnimatedSprite(Image.createImage(pathShadow),
                    FRAME_SHADOW_WIDTH, FRAME_SHADOW_HEIGHT);
            spShadow.defineReferencePixel(FRAME_SHADOW_WIDTH / 2, FRAME_SHADOW_HEIGHT / 2);

            lmgr = new LayerManager();
            lmgr.append(spShadow);
        } catch (IOException ex) {
        }
        //new Thread(this).start();
        //setFullScreenMode(true);

        if (getWidth() < getHeight()) {
            if (getWidth() > acuanDisable) {
                isDisablePortrait = true;
                //System.out.println("PAUSED OY!");
                paused();
            } else {
                isDisablePortrait = false;
            }
        }
        updatePositions();
    }

    public void start() {
        if (!isPaused) {
            running = true;
            thread = new Thread(this);
            thread.start();
        }
    }

    public void run() {
        Graphics g = getGraphics();
        while (running) {
            drawDisplay(g);
            if (loop > 98) {
                running = false;
            }
            try {
                Thread.sleep(speedGame);
            } catch (InterruptedException ie) {
            }
        }
        if (loop > 98) {
            if (getWidth() < getHeight()) {
                if (getHeight() > acuanDisable) {
                } else {
                    midlet.showMenuCanvas(false);
                }
            } else {
                midlet.showMenuCanvas(false);
            }
        }
    }

    protected void drawDisplay(Graphics g) {
        g.setColor(0xffffff);
        g.fillRect(0, 0, getWidth(), getHeight());

        spShadow.nextFrame();
        lmgr.paint(g, 0, 0);
        loop++;
        g.drawImage(imgTitle, xTitle,
                yTitle, Graphics.TOP | Graphics.LEFT);

        g.setColor(0x0000ff);
        yString = spShadow.getY() + (spShadow.getHeight() / 4);
        g.drawString(String.valueOf(loop) + " %", xString, yString,
                Graphics.TOP | Graphics.LEFT);
        //paint disini
        //sampai sini        

        //flush off-screen buffer to display
        flushGraphics();
    }

    /*
    private void dismiss() {
    if (isShown()) {
    host.showMenuCanvas();
    }
    }
     * 
     */
    protected void sizeChanged(int w, int h) {
        super.sizeChanged(w, h);
        if (getWidth() < getHeight()) {
            if (getWidth() > acuanDisable) {
                isDisablePortrait = true;
                //System.out.println("PAUSED OY!");
                paused();
            } else {
                isDisablePortrait = false;
            }
        } else {
            isDisablePortrait = false;
            updatePositions();
            Graphics g = getGraphics();
            drawDisplay(g);
            if (isPaused) {
                int xResume = getWidth() - imgBtnResume.getWidth();
                int yResume = getHeight() - imgBtnResume.getHeight();

                btnResume.setPositionXY(xResume, yResume,
                        Graphics.TOP | Graphics.LEFT);
                btnResume.paint(g);
            }
        }
        updatePositions();
    }

    private void updatePositions() {
        xTitle = getWidth() / 2 - (imgTitle.getWidth() / 2);
        yTitle = getHeight() / 2 - (imgTitle.getHeight());
        xString = getWidth() / 2;
        yString = spShadow.getY() + (spShadow.getHeight() / 4);
        xShadow = getWidth() / 2 - spShadow.getWidth();
        yShadow = getHeight() / 2;
        spShadow.setPosition(xShadow, yShadow);
    }

    protected void hideNotify() {
        super.hideNotify();
        paused();
        int xResume = getWidth() - imgBtnResume.getWidth();
        int yResume = getHeight() - imgBtnResume.getHeight();

        btnResume.setPositionXY(xResume, yResume,
                Graphics.TOP | Graphics.LEFT);
    }

    protected void showNotify() {
        super.showNotify();
        if (!isDisablePortrait) {
            if (isPaused) {
                Graphics g = getGraphics();
                btnResume.paint(g);
                //checkForKeys();
            }
        }
    }

    private void paused() {
        isPaused = true;
        running = false;
        if (isDisablePortrait) {
            Graphics g = getGraphics();
            drawDisablePortrait(g);
        }
    }

    private void drawDisablePortrait(Graphics g) {
        g.drawImage(imgDisablePortrait, 0, 0, Graphics.TOP | Graphics.LEFT);
        flushGraphics();
    }

    protected void pointerPressed(int x, int y) {
        if (isPaused) {
            if (x > btnResume.xImage
                    && x < (btnResume.xImage + btnResume.imageWidth)
                    && y > btnResume.yImage
                    && y < (btnResume.yImage + btnResume.imageHeight)) {
                isPaused = false;
                start();
                repaint();
            }
        }
    }
}
